//
//  VPRefreshExampleViewController.m
//  VPPublicUntilitisForPod
//
//  Created by vernepung on 2017/7/14.
//  Copyright © 2017年 vernepung. All rights reserved.
//

#import "VPRefreshExampleViewController.h"
#import "NSArray+Additional.h"
#import "ConstMacro.h"

@interface VPRefreshExampleViewController ()<VPTableViewViewControllerDelegate>

@end

@implementation VPRefreshExampleViewController
- (void)setStaticDatas {
    self.vp_supportRefresh = YES;
    self.vp_autoRefresh = YES;
    self.vp_displayRefreshControlBottomLine = YES;
}

- (void)setupViews {
    [super setupViews];
    // 更改Frame
    self.vp_tableView.frame = CGRectMake(20, 80, 380, 400);
    self.vp_dataSource = [NSMutableArray arrayWithArray:@[@"是否显示顶部Line",@"是否使用下拉刷新",@"是否支持加载更多",@"是否加载完成",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"]];
}

- (void)vp_pullToRefresh {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.vp_dataSource = [NSMutableArray arrayWithArray:@[@"是否显示顶部Line",@"是否使用下拉刷新",@"是否支持加载更多",@"是否加载完成",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"]];
        [self.vp_tableView reloadData];
    });
}

- (void)vp_pullToLoadMore {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.vp_pageNumber ++;
        self.vp_completed = YES;
        if (self.vp_pageNumber >= 5) {
            self.vp_completed = YES;
        }else{
            for (NSInteger i = 0; i < 10; i++) {
                [self.vp_dataSource addObject:[NSString stringWithFormat:@"第%zd页面的第%zd条数据",self.vp_pageNumber,i]];
            }
        }
        self.title = [NSString stringWithFormat:@"%zd页数据",self.vp_pageNumber];
        
        [self.vp_tableView reloadData];
    });
}


#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"homeCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [self.vp_dataSource objectAtIndexChecked:indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.vp_dataSource.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    self.useRefresh = !self.useRefresh;
    NSInteger row = indexPath.row;
    if (row == 0) {
        self.vp_displayRefreshControlBottomLine = !self.vp_displayRefreshControlBottomLine;
    }else if (row == 1) {
        self.vp_supportRefresh = !self.vp_supportRefresh;
    }else if(row == 2){
        self.vp_supportLoadMore = !self.vp_supportLoadMore;
//        self.vp_completed = YES;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
